var faults=[];//所有故障
var voltage="all";
var tid;
var radiusName={"7级":"radius7","10级":"radius10","12级":"radius12","无地理信息":"radiusUnknown","小于7级":"radiusLe7"};
var radiusMap={"radius7":[],"radius10":[],"radius12":[],"radiusUnknown":[],"radiusLe7":[]};
$(function(){
	tid = $('#tid').val();
	getData();
});
//换电压等级
function onVolChange(){
	radiusMap={"radius7":[],"radius10":[],"radius12":[],"radiusUnknown":[],"radiusLe7":[]};
	voltage=$("#volSelector").val();
	$("#tbTitle").html("");
	if(faultTb){
		faultTb.getTable().destroy();
		$('#faultTb').empty();
		faultTb=null;
	}
	getData();
}
//根据台风id获取相关故障
function getData() {
	if(tid){
		$.getJSON('/rest/TyphoonRestService/getFaultByTyphoonId/' + tid+"/"+voltage,
				function(result) {
			faults = result;
			getChartDatas();
		});
	}
}

function getChartDatas(){
	for(var i in faults){
		if(faults[i].radius=="7"){
			radiusMap["radius7"].push(faults[i]);
		}else if(faults[i].radius=="10"){
			radiusMap["radius10"].push(faults[i]);
		}else if(faults[i].radius=="0"){
			radiusMap["radiusLe7"].push(faults[i]);
		}else if(faults[i].radius=="12"){
			radiusMap["radius12"].push(faults[i]);
		}else{
			radiusMap["radiusUnknown"].push(faults[i]);
		}
	}
	drawChart([['7级',radiusMap["radius7"].length],['10级',radiusMap["radius10"].length],['12级',radiusMap["radius12"].length],['无地理信息',radiusMap["radiusUnknown"].length],['小于7级',radiusMap["radiusLe7"].length]]);
	showTyphoonFault();
}
function showTyphoonFault() {
	var voltages = {'all':'','499':'500kV及以上','220':'220kV','110':'110kV'};
	var str="本次台风共发生"+voltages[voltage]+"故障<span class='count'>"+ faults.length + "</span>次";
	if(faults.length>0){
		str+="，其中7级风圈<span class='count'>"+radiusMap["radius7"].length+"</span>次，10级风圈<span class='count'>"+radiusMap["radius10"].length+"</span>次，12级风圈<span class='count'>"+radiusMap["radius12"].length+"</span>次。";
	}
	$('#typhoonTitle').html(str);
}
var pie;
function drawChart(data){
	pie = new Pie("pie")
	pie.setPie(data);
	var option = pie.getOption();
	option.series[0].label.normal.formatter="{b}:{c}({d}%)";
	option.series[0].radius="60%";
	pie.show();
	pie.getChart().on("click",function(param){
		var name=param.data.name;
		var faultDTO=radiusMap[radiusName[name]];
		getTbData(name,faultDTO);
	});
}
function getTbData(name,faultDTO){
	//{name,faults}
	var faultMap=new Map();
	for(var i in faultDTO){
		var faultRecord=faultDTO[i].faultRecord;
		var faultRecords=faultMap.get(faultRecord.equipment);
		if(!faultRecords){
			faultRecords=[];
		}
		faultRecords.push(faultRecord);
		faultMap.put(faultRecord.equipment,faultRecords);
	}
	var data=[];
	for(var i in faultMap.keys){
		var equipment=faultMap.keys[i];
		var count=faultMap.get(equipment).length;
		data.push([equipment,count]);
	}
	data.sort(function(x,y){
		return y[1]-x[1];
	});
	initTb(name,data);
}
var faultTb;
function initTb(name,data){
	$("#tbTitle").html(name+"风圈故障");
	if(!faultTb){
		var opt = MyTable.getDefaultOptions();
		opt.paging = false;
		opt.scrollY = '254px';
		var columns = [ '故障设备', '故障次数'];
		faultTb = new MyTable('faultTb', columns, opt);
		faultTb.setDataSet(data);
		faultTb.show();
	}else{
		faultTb.reload(data);
	}
}
function resizeHtml() {
	if (pie) {
		pie.getChart().resize();
	}
	if (faultTb) {
		faultTb.getTable().draw();
	}
}